<template>
  <div class="department">
    <el-button
      type="primary"
      style="margin: 20px 0"
      @click="dialogVisible = true"
      >创建一级部门</el-button
    >

    <div class="block">
      <el-tree :data="data" node-key="id" :props="props">
        <div class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <div class="custom-tree-node-button">
            <el-button type="text" @click="onAppend(data)">
              添加下级部门
            </el-button>
            <el-button
              type="text"
              style="color: red"
              @click="onDelete(data.id)"
            >
              删除部门
            </el-button>
          </div>
        </div>
      </el-tree>
    </div>
    <el-dialog title="创建一级部门" :close-on-click-modal="false" :visible.sync="dialogVisible" width="30%">
      <div class="dialogVisibledivone">
        <span>一级部门</span>
        <el-input
          v-model="departmentnameone"
          placeholder="请输入一级部门名称"
          clearable
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onAddConfirmOne">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加子部门"
      :visible.sync="dialogVisiblechild"
      :close-on-click-modal="false"
      width="30%"
    >
      <div>
        <div class="dialogVisibledivone" style="margin-bottom: 20px">
          <span>上级部门</span>
          <el-input
            disabled
            v-model="parent.title"
            placeholder="请输入一级部门名称"
            clearable
          ></el-input>
        </div>
        <div class="dialogVisibledivone">
          <span>子级部门</span>
          <el-input
            v-model="departmentname"
            placeholder="请输入子部门名称"
            clearable
          ></el-input>
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblechild = false">取 消</el-button>
        <el-button type="primary" @click="onConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminRecruitmentInformationGetProfession,
  postAdminRecruitmentInformationAddDepartment,
  postAdminRecruitmentInformationdelDepartment
} from "@/api";
export default {
  data() {
    return {
      dialogVisiblechild: false,
      dialogVisible: false,
      props: {
        label: "title",
        children: "position",
      },
      departmentnameone: "",
      departmentname: "",
      data: [],
      parent: "",
    };
  },
  activated() {
    this.postAdminRecruitmentInformationGetProfession();
  },
  methods: {
    onDelete(id) {
      let obj = {
        id,
      };
      this.$confirm("确定删除该部门?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postAdminRecruitmentInformationdelDepartment(obj).then((res) => {
            this.postAdminRecruitmentInformationGetProfession();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    postAdminRecruitmentInformationGetProfession() {
      postAdminRecruitmentInformationGetProfession().then((res) => {
        console.log("部门管理", res);
        this.data = res.data;
      });
    },
    onAppend(e) {
      console.log("添加部门----》", e);
      this.parent = e;
      this.dialogVisiblechild = true;
    },
    onConfirm(){
      if (this.departmentname) {
        let obj = {
          fid: this.parent.id,
          title: this.departmentname,
        };
        postAdminRecruitmentInformationAddDepartment(obj).then(res=>{
          this.$message.success("创建成功")
          this.dialogVisiblechild = false
          this.postAdminRecruitmentInformationGetProfession();
        })
      }else{
        this.$message.warning("请输入子级部门名称")
      }
    },
    onAddConfirmOne() {
      if (this.departmentnameone) {
        let obj = {
          title: this.departmentnameone,
        };
        postAdminRecruitmentInformationAddDepartment(obj).then(res=>{
          this.$message.success("创建成功")
          this.dialogVisible = false
          this.postAdminRecruitmentInformationGetProfession();
        })
      }else{
        this.$message.warning("请输入一级部门名称")
      }
    },

    remove(node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex((d) => d.id === data.id);
      children.splice(index, 1);
    },
  },
};
</script>

<style lang="scss" scoped>
.dialogVisibledivone {
  display: flex;
  justify-content: center;
  align-items: center;
  /deep/ .el-input {
    width: 360px;
    margin-left: 20px;
  }
}

.department {
  background: #fff;
  padding: 20px;
  border-radius: 4px;
  /deep/ .el-tree-node__content {
    height: 50px;
    border-bottom: 1px solid #f0f1f7;
  }
}
.custom-tree-node {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  &-button {
    display: flex;
    /deep/ .el-button {
      margin-left: 20px;
    }
  }
}
</style>